#!/usr/bin/python
# -*- coding:utf-8 -*-

import re
import sentry_sdk
from flask import g


ignore_log = [
    "结果详情为空， 任务处理状态设置为失败",
    "\[02\]监测结果处理",
    "\[01\]更新结果失败",
    "\[0F\]\[余额更新\]查询余额Mongo记录失败",
    "\[0F\]\[余额更新\]未购买套餐",
    "\[0F\]\[余额更新\]套餐已过期",
    "加黑请求发送失败",
    "{'code': 401",
    "{'code': 403",
    "{'code': 404",
]

regex_str = re.compile(r"|".join(ignore_log))


def before_send(event, hint):
    try:
        sentry_sdk.set_user({"id": g.user.id, "username": g.user.username, "ip": g.ip})
    except Exception:
        sentry_sdk.set_user({"id": g.ip, "username": g.ip, "ip": g.ip})
    message = event.get("logentry", {}).get("message", "")
    if regex_str.search(message):
        return None
    return event
